/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:07
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.repository;

import com.joysuch.wwyt.edu.entity.EduCourseware;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.List;

/**
 * EduCoursewareDao
 *
 */
public interface EduCoursewareDao extends JpaRepository<EduCourseware, Long>, QuerydslPredicateExecutor<EduCourseware> {
    public Page<EduCourseware> findAll(Specification<EduCourseware> spec, Pageable pageable);

    public EduCourseware save(EduCourseware bean);

    public void delete(EduCourseware bean);

    @Query("select count(m) from EduCourseware m where code = ?1")
    long countOfCode(String code);

    @Query("select count(m) from EduCourseware m where code = ?1 and id != ?2")
    long countOfCodeExcludeId(String code, Long id);

    @Query("select count(m) from EduCourseware m where name = ?1")
    long countOfName(String name);

    @Query("select count(m) from EduCourseware m where name = ?1 and id != ?2")
    long countOfNameExcludeId(String name, Long id);

    @Query("select name from EduCourseware m where id = ?1")
    public String getNameById(Long coursewareId);

    @Query("select m.id from EduCourseware m where m.id in(?1) and m.deleteFlag='0' and m.format= 'mp4' ")
    List<Long> findVideo(List<Long> coursewareIds);

    @Query(value = "SELECT IFNULL(MAX(minute),0) FROM `wwyt_edu_courseware` WHERE doc_type = ?1",nativeQuery = true)
    int selectBydocTypeMaxMinute(String docType);
}
